package com.wisdytech.ccdserver.matchwarning.serveice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wisdytech.ccdserver.common.es.EsOperationUtils;
import com.wisdytech.ccdserver.matchwarning.dao.*;
import com.wisdytech.ccdserver.matchwarning.model.WarningEquipment;
import com.wisdytech.ccdserver.matchwarning.model.WarningMatch;
import com.wisdytech.ccdserver.matchwarning.model.WarningMesProduction;
import com.wisdytech.ccdserver.matchwarning.model.WarningMesSandstone;
import com.wisdytech.ccdserver.matchwarning.serveice.IWarningMatchService;
import com.wisdytech.ccdserver.matchwarning.serveice.IWarningMesProductionService;
import com.wisdytech.common.bean.BaseModel;
import com.wisdytech.common.bean.BasePage;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunjinxin
 * @date 2024/5/20
 */
public class WarningMatchServiceImpl extends ServiceImpl<IWarningMatchDao, WarningMatch> implements IWarningMatchService {

    @Resource
    private IWarningMesProductionDao warningMesProductionDao;

    @Resource
    private IWarningEquipmentDao warningEquipmentDao;

    @Resource
    private IWarningMesSandstoneDao warningMesSandstoneDao;

    @Autowired
    private EsOperationUtils esOperationUtils;

    @Override
    public IPage<WarningMatch> getWarningMatchList(Filter filter) {

        Page<WarningMatch> page = BasePage.getPage(filter);
        IPage<WarningMatch> warningMesSandstoneIPage = baseMapper.getWarningMatchList(page,filter.getQueryMap());
        return  warningMesSandstoneIPage;
    }

    @Override
    public void countMatch() {
        Map<String,Object> queryMap = new HashMap<>();

        String start = "2024-05-16 16:00:00";
        String end = "2024-05-16 17:00:00";

        String matchKeyPre = "2024051608";

        queryMap.put("productionDate_start",start);
        queryMap.put("productionDate_end",end);

        //统计mes产量
        List<WarningMesProduction> warningMesProductionList = warningMesProductionDao.getWarningMesProductionList(queryMap);
        Map<String, Integer> productionSum = warningMesProductionList.stream()
                .collect(Collectors.groupingBy(WarningMesProduction::getEquipmentCode,
                        Collectors.summingInt(WarningMesProduction::getProductionNum)));

        //查询需要报警的设备信息
        LambdaQueryWrapper<WarningEquipment> equipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        equipmentLambdaQueryWrapper.eq(BaseModel::getDelFlag,"0");
        List<WarningEquipment> warningEquipmentList = warningEquipmentDao.selectList(equipmentLambdaQueryWrapper);

        LambdaQueryWrapper<WarningMesSandstone> mesSandstoneLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<WarningMesSandstone> warningMesSandstoneList= warningMesSandstoneDao.selectList(mesSandstoneLambdaQueryWrapper);

        Map<String,String> mesSandstone = warningMesSandstoneList.stream()
                .collect(Collectors.toMap( WarningMesSandstone::getEquipmentCode,
                WarningMesSandstone::getBucketDirectory,
                (oldValue, newValue) -> newValue));
        Map<String,String> mesSandstoneBucket = warningMesSandstoneList.stream()
                .collect(Collectors.toMap( WarningMesSandstone::getEquipmentCode,
                        WarningMesSandstone::getBucketCode,
                        (oldValue, newValue) -> newValue));

        for (WarningEquipment warningEquipment : warningEquipmentList) {
            if(productionSum.containsKey(warningEquipment.getEquipmentCode())){
                WarningMatch warningMatch = new WarningMatch();

                warningMatch.setEquipmentCode(warningEquipment.getEquipmentCode());
                warningMatch.setEquipmentName(warningEquipment.getEquipmentName());
                warningMatch.setFactoryCode(warningEquipment.getFactoryCode());
                warningMatch.setProductionNum(productionSum.get(warningEquipment.getEquipmentCode()));
                warningMatch.setBucketDirectory(mesSandstone.get(warningEquipment.getEquipmentCode()));
                warningMatch.setBucketCode(mesSandstoneBucket.get(warningEquipment.getEquipmentCode()));
                warningMatch.setIsDing("0");
                warningMatch.setWarnDate(matchKeyPre);
                warningMatch.setEquipmentNum(warningEquipment.getMatchNum());
                warningMatch.setModel(warningEquipment.getModel());
                warningMatch.setOperationCode(warningEquipment.getOperationCode());
                warningMatch.setOriginMtimeBegin(start);
                warningMatch.setOriginMtimeEnd(end);
                warningMatch.setMatchKey(matchKeyPre+warningEquipment.getEquipmentCode());

//                try {
//                    esOperationUtils.getImageNumByDirectory(null);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
                BaseUtils.setDefaultFiledsBaseModel(warningMatch, "sys", true);
                baseMapper.insert(warningMatch);
            }
        }

    }
}
